package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.CategoryFindNullException;
import com.sky.exception.CategoryFomatException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.CategoryResult;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: HF62sky
 * @package: com.sky.service.impl
 * @className: CategoryServiceImpl
 * @author: SunGang
 * @description: TODO
 * @date: 2023/12/3 17:45
 * @version: 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public CategoryResult findByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Category category = new Category();
        category.setName(categoryPageQueryDTO.getName());
        category.setType(categoryPageQueryDTO.getType());
        Page<Category> page = categoryMapper.seleceByPage(category);
        if (page.size() == 0) {
            throw new CategoryFindNullException("未找到相关分类");
        }
        CategoryResult categoryResult = new CategoryResult();
        categoryResult.setTotal(page.getTotal());
        categoryResult.setRecords(page.getResult());
        return categoryResult;
    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.insert(category);
    }

    @Override
    public void modifyStatus(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.updateStatusById(category);
    }

    @Override
    public List<Category> findByType(Integer type) {
        Category category = new Category();
        category.setType(type);
        List<Category> cate = categoryMapper.SelectByType(category);
        return cate;
    }

    @Override
    public void modifyCategoryById(CategoryDTO categoryDTO) {
        checkCategory(categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.updateStatusById(category);

    }

    @Override
    public void deleteById(Long id) {
        Long count = dishMapper.seleceByCategoryId(id);
        if (count > 0) {
            throw new CategoryFomatException("分类下面有产品不可删除");
        }
        Long count2 = setmealMapper.seleceByCategoryId(id);
        if (count2 > 0) {
            throw new CategoryFomatException("分类下面有产品不可删除");
        }
        categoryMapper.deleteById(id);
    }

    private void checkCategory(CategoryDTO categoryDTO) {
        if (Objects.isNull(categoryDTO.getName()) || categoryDTO.getName()
                .length() < 2 || categoryDTO.getName()
                .length() > 20) {
            throw new CategoryFomatException("分类名称不符,请输入2-20个字符");
        }
        if (Objects.isNull(categoryDTO.getSort())) {
            throw new CategoryFomatException("排序不能为空");
        }
        if (!categoryDTO.getSort().toString().matches("[1-9]|[1-9][0-9]")) {
            throw new CategoryFomatException("排序只能输入整数类型");
        }
    }
}
